package com.hws.leetcode.Day_12_28;

import java.util.Arrays;

/**
 * ClassName:leetcode_34
 * Package:com.hws.leetcode.Day_12_28
 * Description:
 *
 * @ date:2021/12/28 13:59
 * @ author:hws
 */
public class leetcode_34 {

    public int left_bound(int[] nums, int target) {
        if (nums.length == 0) {
            return -1;
        }
        int left = 0;
        int right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                right = mid;
            } else if (nums[mid] > target) {
                right = mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            }
        }
        if (left == nums.length) {
            return -1;
        }
        return nums[left] == target ? left : -1;
    }

    public int right_bound(int[] nums, int target) {
        if (nums.length == 0) {
            return -1;
        }
        int left = 0;
        int right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            }
        }
        if (left == 0) {
            return -1;
        }
        return nums[left - 1] == target ? (left - 1) : -1;
    }

    public int[] searchRange(int[] nums, int target) {
        int[] result = new int[2];
        result[0] = left_bound(nums,target);
        result[1] = right_bound(nums, target);
        return result;
    }

    public static void main(String[] args) {
        leetcode_34 lc = new leetcode_34();
        int[] nums = new int[]{5,7,7,8,8,10};
        int target = 6;
        System.out.println(Arrays.toString(lc.searchRange(nums, target)));
    }
}
